package day2.HomeWork;

import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class homework {
    public static void main(String[] args) throws IOException {
        demo6();
    }

    public static void demo1() {
        for (int i = 2; i <= 100; i++) {
            boolean isNum = true;
            for (int j = 2; j < i; j++) {
                if (i % j == 0) {
                    isNum = false;
                    break;
                }
                System.out.println(i);
            }
            if (isNum) {
                System.out.println(i);
            }
        }
    }

    //选择排序
    public static void demo2() {
        int[] arr = {6, 3, 12, 78, 9, 23, 99, 1};
        for (int i = 0; i < arr.length - 1; i++) {
            int min = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[min]) {
                    min = j;
                }
            }
            /*int temp = arr[min];
            arr[min] = arr[i];
            arr[i] = temp;*/
            arr[min] = arr[min] + arr[i];
            arr[i] = arr[min] - arr[i];
            arr[min] = arr[min] - arr[i];
        }
        for (int i : arr) {
            System.out.println(i);
        }
    }

    public static void demo3() throws IOException {
        File f = new File("d://wuxi.D//video//6.24上午.mp4");
        try (FileInputStream fis = new FileInputStream(f);
             FileOutputStream fos = new FileOutputStream("d://a//NewVideo.mp4");
             BufferedInputStream bis = new BufferedInputStream(fis);
             BufferedOutputStream bos = new BufferedOutputStream(fos)
        ) {
            long start = System.currentTimeMillis();
            long sum = f.length();  //视频的总字节数
            long read = 0;  //保存已经读取的字节数
            int n = 1;
            byte[] bs = new byte[1024];
            int len = 0;
            while ((len = bis.read(bs)) != -1) {
                read += len;
                bos.write(bs, 0, len);
                if (read >= sum / 100 * n) {
                    long end = System.currentTimeMillis();
                    System.out.println("复制了 " + (n * 1) + "% , 耗时:" + (end - start) + "ms");
                    n++;
                }
            }
            long end = System.currentTimeMillis();
            System.out.println("复制成功,耗时:" + (end - start) + "ms");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void demo4() throws IOException {
        File f = new File("d://a//a.txt");
        int resultA = 0;
        int resulta = 0;
        try (FileInputStream fis = new FileInputStream(f);) {
            for (int i = 0; i < f.length(); i++) {
                // fis.read();
                char n = (char) fis.read();
                if (n == 'A') {
                    resultA++;
                } else if (n == 'a') {
                    resulta++;
                }
            }
            System.out.println("A次数:" + resultA);
            System.out.println("a次数:" + resulta);
            System.out.println("总次数:" + (resulta + resultA));
        } catch (Exception e) {
            e.printStackTrace();
        }


        /*FileInputStream fis = new FileInputStream("d://a//a.txt");
        byte[] b = new byte[100];
        int Acount = 0;
        int acount = 0;
        for (int i = 0; i < b.length; i++) {
            int n = fis.read();
            if (n == 97) {
                Acount++;
            } else if (n == 65) {
                acount++;
            }
        }
        System.out.println(Acount);
        System.out.println(acount);*/
    }

    public static void demo5() {
        try (FileInputStream fis = new FileInputStream("d://a//a.txt");
             FileOutputStream fos = new FileOutputStream("d://a//b.txt")
        ) {
            //统计每个字符次数: map集合(key(char) : value(次数int))
            Map<Character, Integer> map = new HashMap<>();
            byte[] bs = new byte[1024];
            int len = 0;
            while ((len = fis.read(bs)) != -1) {
                String s = new String(bs, 0, len);
                for (int i = 0; i < s.length(); i++) {
                    char h = s.charAt(i);
                    if (map.containsKey(h)) {
                        map.put(h, map.get(h) + 1); //如果key存在 次数+1
                    } else map.put(h, 1);   //如果key不存在 次数=1
                }
            }
            //处理写入的 结果字符串
            StringBuilder sb = new StringBuilder("最终的结果是:字符(次数)\n");
            for (Character key : map.keySet()) {
                Integer value = map.get(key);
                sb.append(key + "(" + value + ") ,");
            }
            //写入结果字符串
            fos.write(sb.toString().getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void demo6() {
        try (FileReader fis = new FileReader("d://a//a.txt");
             FileWriter fos = new FileWriter("d://a//b.txt")
        ) {
            //统计每个字符次数: map集合(key(char) : value(次数int))
            Map<Character, Integer> map = new HashMap<>();
            char[] bs = new char[1024];
            int len = 0;
            while ((len = fis.read(bs)) != -1) {
                for (int i = 0; i < len; i++) {
                    char h = bs[i];
                    if (map.containsKey(h)) {
                        map.put(h, map.get(h) + 1); //如果key存在 次数+1
                    } else map.put(h, 1);   //如果key不存在 次数=1
                }
            }
            //处理写入的 结果字符串
            StringBuilder sb = new StringBuilder("最终的结果是:字符(次数)\n");
            for (Character key : map.keySet()) {
                Integer value = map.get(key);
                sb.append(key + "(" + value + ") \n");
            }
            //写入结果字符串
            fos.write(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}